import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2022-10-21
 * Time: 19:48
 */
class Person{
    private String name;//实例成员变量
    private int age;
    private String sex;

    public Person() {
        System.out.println("I am Person init()!");
    }

    //实例代码块
    {
        this.name = "bit";
        this.age = 12;
        this.sex = "man";
        System.out.println("I am instance init()!");
    }

    public void show(){
        System.out.println("name: "+name+" age: "+age+" sex: "+sex);
    }

}
public class TestDemo {
    public static double avg(int[] array){
        int ret = array.length;
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return (double)(sum/ret);
    }
    public static int sum(int[] array){
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return sum;
    }
    public static int[] transfrom(int[] array){
        int[] ret = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = 2 * array[i];
        }
        return ret;
    }
    public static void print(int[] array){
        for (int x:array) {
            System.out.print(x+" ");
        }
    }
    public static void bubblesort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-i-1; j++) {
                if (array[j] > array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
            if (!flg){
                return;
            }
        }
    }
    public static boolean isSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            if (array[i] > array[i+1]){
                return false;
            }
        }
        return true;
    }
    public static int binarySearch(int[] array,int n) {
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (array[mid] > n) {
                right = mid - 1;
            } else if (array[mid] < n) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {

    }
    public static void main9(String[] args) {
        int[] array = {1,2,4,7,15,17,21,32,55,88};
        int ret = binarySearch(array,32);
        System.out.println(ret);
    }
    public static void main8(String[] args) {
        int[] array = {1,3,5,7,9,11,13};
        System.out.println(isSort(array));
    }
    public static void main7(String[] args) {
        int[] array = {1,4,3,6,2,5,9,7,8};
        bubblesort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main6(String[] args) {
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        for (int x:array) {
            System.out.print(x+" ");
            if (x%10 == 0) {
                System.out.println();
            }
        }
    }
    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        print(array);
    }
    public static void main4(String[] args) {
        int[] array = {1,2,3};
        int[] ret2 = transfrom(array);
        System.out.println(Arrays.toString(ret2));
    }
    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        System.out.println(sum(array));
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8};
        System.out.println(avg(array));
    }
    public static void main1(String[] args) {
        Person p1 = new Person();
        p1.show();
    }
}
